<?php
	function mysql_escape_data( $result ){
		return function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() 
			?  $result  : addslashes($result);	
	}
	
	function get( $qr_name, $qr_default = NULL, $accept_array = false, $method='REQUEST' ){
		if( $method == "POST" ){
			$method = $_POST;
		}else if( $method == "GET" ){
			$method = $_GET;	
		}else{
			$method = $_REQUEST;
		}
		
		if(isset($method[$qr_name])){
			$result = $method[$qr_name];
			if( is_array($result)){
				if( !$accept_array )
					return $qr_default;
				foreach( $result as $k => $value ){
					$result[$k] = mysql_escape_data($value);
				}
				return $result;	
			}
			return mysql_escape_data($result);	
		}
		if( is_array( $qr_default ) ){
			foreach( $qr_default as $k => $value ){
				$qr_default[$k] = mysql_escape_data($qr_default);
			}
			return $qr_default;
		}
		return addslashes(stripslashes($qr_default));	
	}
	
	function _get( $qr_name, $qr_default = NULL, $accept_array = false, $method='REQUEST' ){
		_e( get($qr_name, $qr_default, $accept_array, $method) );
	}
	
	function get_escape( $qr_name, $qr_default = NULL, $accept_array = false, $method='REQUEST'){
		return html_escape( stripslashes(get( $qr_name, $qr_default, $accept_array, $method)));		
	}
	
	
	/*Get request from POST*/
	function getPost( $qr_name, $qr_default = NULL, $accept_array = false ){
		return get( $qr_name, $qr_default,$accept_array,'POST');	
	}

	/*Get a request with int value from global*/
	function getInt($qr_name, $qr_default = NULL){
		$result = get($qr_name, $qr_default, false );
		if(!is_numeric( $result ))	return $qr_default;
		return $result;
	}
	
	function getPostInt($qr_name, $qr_default=NULL){
		$result = get($qr_name, $qr_default, false,'POST' );
		if(!is_numeric( $result ))	return $qr_default;
		return $result;
	}

	function getArray( $qr_name, $qr_default = array(), $filter=NULL ){
		$result = get( $qr_name, $qr_default, true );
		if( is_array($result))
			return $result;
		if( is_array( $qr_default ) ){
			foreach( $qr_default as $k => $value ){
				$qr_default[$k] = mysql_escape_data($qr_default);
			}
			return $qr_default;
		}	
	}
		
	function isPost($name=NULL){
		if( $name ){
			return $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST[$name]); 
		}
		return $_SERVER['REQUEST_METHOD'] == 'POST';
	}
	
	function isAjax(){
		return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';	
	}
	
	function redirect( $url, $abs = false ){
		/* Keep _json & _ajax */
		if( isset( $_REQUEST['_json'] ) ){
			$url .= preg_match("/\?/ui",$url,$match) ? "&_json=yes" : "?_json=yes";
		}
		
		if( $abs ) $url = BASE_URL.$url;
		header("Location: $url");
		exit;	
	}

	function search_key_map(){
		return array(
			'like'				=>	'Bao gồm',
			'no-like'			=>	'Không bao gồm',
			'equal'				=>	'Bằng',
			'unequal'			=>	'Khác',
			'greater'			=>	'Lớn hơn',
			'greater-equal'		=>	'Lớn hơn hoặc bằng',
			'less'				=>	'Nhỏ hơn',
			'less-equal'		=>	'Nhỏ hơn hoặc bằng',
			'in'				=>	'Thuộc',
			'no-in'				=>	'Không thuộc',
			'null'				=>	'Rỗng',
			'no-null'			=>	'Không rỗng',
			'start'				=>	'Bắt đầu bằng',
			'no-start'			=>	'Không bắt đầu bằng',
			'end'				=>	'Kết thúc bằng',
			'no-end'			=>	'Không kết thúc bằng',
			'between'			=>	'Khoảng'
		);
	}
	
	function join_sql_search( $field, $key, $value ){
		switch( strtolower( $key ) ){
			case 'equal':
				return "$field = '$value'";	
			case 'greater-equal':
				return "$field >= '$value'";
			case 'greater':
				return "$field > '$value'";
			case 'less':
				return "$field < '$value'";
			case 'less-equal':
				return "$field <= '$value'";
			case 'unequal':
				return "$field <> '$value'";	
			case 'not-like':	
				return "$field NOT LIKE '%$value%'";
			case 'like':
				return "$field LIKE '%$value%'";	
			case 'start':
				return "$field LIKE '$value%'";	
			case 'end':
				return "$field LIKE '%$value'";	
			case 'no-start':
				return "$field NOT LIKE '$value%'";	
			case 'no-end':
				return "$field NOT LIKE '%$value'";	
			case 'in':
				return "$field IN ".get_in_mysql(explode(',',$value));
			case 'no-in':
				return "$field NOT IN ".get_in_mysql(explode(',',$value));	
			case 'null':
				return "$field IS NULL";	
			case 'no-null':
				return "$field IS NOT NULL";	
			case 'between':
				list($start,$end) = explode(',',$value);
				return $field." BETWEEN '".addslashes($start)."' AND ".addslashes($end);	
			default:			
				return "$field='$value'";	
		}			
	}